// pages/me/detail/detail.js
import {fetchData,uploadMinio,get,postRequest} from '../../../utils/request'
var app = getApp();
const baseUrl = app.globalData.root;
Page({

  /**
   * 页面的初始数据
   */
  data: {
  userInfo:{}//用户信息
  },
  isLoginPermission(){
    let user=wx.getStorageSync("user");
    console.log(user);
    if(user!=null){
        this.setData({
            userInfo:user
        })
    }
  },
async  uploadAvatar(url){
    let avatar =await uploadMinio(url)
   if(avatar){
       this.bindWx(avatar.url)
   }
},
  async onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
   this.uploadAvatar(avatarUrl)
  },
  async bindWx(url){
      const {openid} = wx.getStorageSync('userInfo')
      const {userId} = wx.getStorageSync('user')
       const result = await postRequest(`/wx/common/bindWx`,{openid:openid,userId:userId,avatar:url})
     if(result){
         wx.showToast({
           title: '绑定成功!',
         })
     }
  },
  logout(){
  fetchData('/logout',"","GET",res=>{
   if(res.data.code==200){
    wx.showToast({
        icon: 'success',
        title: "退出成功!!",
        duration: 1500,
        mask: true,
    })
       wx.redirectTo({
         url: '/pages/login/login',
       })
   }
  },err=>{

  })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  this.isLoginPermission()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})